package com.android.fragmentdemo
import android.content.Context
import android.graphics.Point
import android.os.Build
import android.os.Bundle
import android.view.WindowManager
import com.hengfoxwolf.commonbase.base.BaseActivity2
import com.android.fragmentdemo.databinding.ActivityScreenPixelBinding

class ScreenPixelActivity :
    BaseActivity2<ActivityScreenPixelBinding>(ActivityScreenPixelBinding::inflate) {

    override fun init(savedInstanceState: Bundle?) {

        val density = resources.displayMetrics.density
        val densityDpi = resources.displayMetrics.densityDpi
        val xdpi = resources.displayMetrics.xdpi
        val ydpi = resources.displayMetrics.ydpi

        val content =
            "widthPixels:${getMaxPoint().x};\nheightPixels:${getMaxPoint().y};\ndensity:${density};\ndensityDpi:${densityDpi};\nxdpi:${xdpi};\nydpi:${ydpi}"
        mBinding.tvPixelContent.text = content
    }

    //获取屏幕真实宽高
    private fun getMaxPoint() : Point {
        val point = Point()
        val windowManager = getSystemService(Context.WINDOW_SERVICE) as WindowManager
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
            point.x = windowManager.maximumWindowMetrics.bounds.width()
            point.y = windowManager.maximumWindowMetrics.bounds.height()
        } else {
            val display = windowManager.defaultDisplay
            display.getRealSize(point)
        }
        return point
    }
}